<?php
class ChPassCtl extends BaseCtl {
	public function ChPassCtl(){
		parent::BaseCtl();
	}
	
	public function submit(){
		try {
			// get passwords, verify they are correct
			// then get the sticker for readonly
			// determine the password that will be persisted
			// persist the password
			$form = $this->getChPassForm();
			
			$id = $form->getId();
			$pass = trim($form->getPass());
			$repeat = trim($form->getRepeat());
			$readonly = $form->isReadonly();
			
			if($pass == $repeat){
				$service = new Service();
				
				$val = strlen($pass) == 0 ? '' : md5($pass);
				
				if($readonly){
					$service->changeReadonlyPassword($id, $val);
				}
				else {
					$service->changeEditPassword($id, $val);
				}
				
				$this->redirect('users.php');
			}
			else {
				throw new Exception('The provided passwords don\'t match');
			}
			
			
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
	public function cancel(){
		try {
			$this->redirect('users.php');
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
    
	public function isPostback(){
		return 'chpass' == $this->getPost('pagename');
	}

    public function loadData(){
    	$form = $this->getChPassForm();
    	
		$form->setId($this->getPost('id'));
		$form->setName($this->getPost('name'));
		$form->setLogin($this->getPost('login'));
		$form->setPass($this->getPost('pass'));
		$form->setRepeat($this->getPost('repeat'));
		
		$form->setReadonly('on' == $this->getPost('readonly'));
    }
} 
?>